package com.pr.makfilm.core.cmp.web;

import com.pr.makfilm.core.validator.DateFormatValidator;
import org.apache.wicket.Component;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.validation.IValidator;

/**
 * Created with IntelliJ IDEA.
 * User: Barbara
 * Date: 29.10.13
 * Time: 20:53
 * To change this template use File | Settings | File Templates.
 */
public class BootstrapDatePicker extends AbstractBootstrapComponent
{
   private static final String CSS_CLASS = "date-picker";

   private TextField textField;

   public BootstrapDatePicker(String id, IModel<String> model)
   {
      super(id);
      cssClass += " " + CSS_CLASS;
      textField = new TextField<String>("datepicker", model);
      textField.add(new DateFormatValidator());
      container.add(textField);
   }

   @Override
   public void setValidation(boolean required)
   {
      textField.setRequired(required);
   }

   public void addValidator(IValidator validator)
   {
      textField.add(validator);
   }

   @Override
   public void renderHead(IHeaderResponse response)
   {
      response.render(OnDomReadyHeaderItem.forScript("datepicker();"));
   }

   @Override
   public Component getField()
   {
      return textField;
   }
}
